//
//  StatusCell.swift
//  Weibo
//
//  Created by 潇 on 15/12/23.
//  Copyright © 2015年 潇. All rights reserved.
//

import UIKit
import SnapKit

class StatusCell: UITableViewCell {

    var bottomViewTopConstraints: Constraint?
    
    //定义微博模型属性
    var status: Status? {
        didSet {
            //给顶部视图 设置模型数据
            topView.status = status

            
            //卸载掉顶部约束
            bottomViewTopConstraints?.uninstall()
            //需要根据数据模型 提供的数据 判断是否显示转发微博视图
            if let reteetedSatus = status?.retweeted_status {
                //是转发微博
                //1.修改约束
                bottomView.snp_makeConstraints { (make) -> Void in
                    self.bottomViewTopConstraints = make.top.equalTo(transmitView.snp_bottom).constraint
                }
                //设置转发微博的数据源
                transmitView.retweetedStatus = reteetedSatus
                //显示转发微博
                 transmitView.hidden = false

            }else {
                //原创微博
                bottomView.snp_makeConstraints { (make) -> Void in
                    self.bottomViewTopConstraints = make.top.equalTo(topView.snp_bottom).constraint
                }
                //隐藏转发微博视图
                transmitView.hidden = true
            }
        }
    }
    
    //cell入口
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setupUI()
        
        //设置 cell 选中的样式
        selectionStyle = .None
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    //自定义 cell
    private func setupUI() {
        //使用 contentView添加子视图
        contentView.addSubview(topView)
        contentView.addSubview(bottomView)
        contentView.addSubview(transmitView)
        
        //设置约束
        //顶部视图的约束
        topView.snp_makeConstraints { (make) -> Void in
//        make.top.equalTo(contentView.snp_top)
//        make.left.equalTo(contentView.snp_left)
//        make.right.equalTo(contentView.snp_right)
        make.top.left.right.equalTo(contentView)
        
        //设置高度,为了测试
//        make.height.equalTo(50)
        }
        
        //转发微博的约束
        transmitView.snp_makeConstraints { (make) -> Void in
            make.left.right.equalTo(contentView)
            make.top.equalTo(topView.snp_bottom)
            //便于前期调试的高度
//            make.height.equalTo(50)
        }
        
        //底部视图的约束
        bottomView.snp_makeConstraints { (make) -> Void in
            make.left.right.equalTo(self)
            
            self.bottomViewTopConstraints = make.top.equalTo(transmitView.snp_bottom).constraint
            make.height.equalTo(40)
        }
        
        
        //给 ContentView 设置约束,非常关键的一点
        contentView.snp_makeConstraints { (make) -> Void in
            make.top.left.right.equalTo(self)
            make.bottom.equalTo(bottomView.snp_bottom)
        }

    }
    
    //懒加载
    //顶部视图
    private lazy var topView: StatusCellTopView = StatusCellTopView()
    
    //准发微博
    private lazy var transmitView: StatusTransmitView = StatusTransmitView()
    
    //底部视图
    private lazy var bottomView: StatusCellBottomView = StatusCellBottomView()
    
}
